apiready = function() {
    seajs.use(['../../V', "../../api", "../../jquery", "../../app", "../../service", "../../dialog"], function(V, $api, $, app, service, dialog) {
        var vm = {
            el: '.container',
            data: {
                order_id: '',
                goods_id: '',
                orderDetailList: '',
                goods_arr: [],
                refund_arr: '',
                refundPrice: '',
                order_return_id: '',
                shopId: '',
            },
            mounted: function() {
              api.addEventListener({
                  name:'swiperight'
              }, function(ret, err){
                api.closeFrame({
                    animation: {
                        type: 'push',
                        subType: "from_left",
                        duration: 300
                    },
                });
              });
            },
            methods: {
                //--获取订单详情
                orderDetailGet: function(callback) {
                    // var shopId = api.pageParam.shopId; // 商店id
                    var unionid = $api.getStorage("shop_unionid"); // 用户id
                    var that = this;

                    app.ajax(service['orderDetailGet'], {
                            data: {
                                values: {
                                    unionid: unionid,
                                    order_id: this.order_id
                                }
                            }
                        },
                        function(res) {
                            if (res.code) {
                                alert("错误代码code：" + res.code + "\n错误信息：" + res.message)
                            } else {
                                callback(res)
                                console.log("获取订单详情成功!");
                            }
                        });
                },
                //--跳转到商品预览页面
                openGoodsDetail: function(goodsId) {
                    app.openWin('buyGoods', {
                        goodsId: goodsId,
                        isSeller: 1
                    })
                },
                //--点击图片查看大图
                biggerImg: function(inds) {
                    var that = this;
                    var photoBrowser = api.require('photoBrowser');
                    photoBrowser.open({
                        images: this.refund_arr.refund_images,
                        // placeholderImg: 'widget://res/img/apicloud.png',
                        bgColor: '#000',
                        activeIndex: inds
                    }, function(ret, err) {
                        if (ret) {
                            if (ret.eventType == 'click') {
                                photoBrowser.close();
                                photoBrowser.clearCache();
                            }
                        } else {
                            // alert(JSON.stringify(err));
                        }
                    });
                },
                //--底下按钮
                orderModBtn: function(mod, order_id, user_info) {
                    var that = this;
                    var unionid = $api.getStorage('shop_unionid');

                    switch (mod) {
                        case "shopChat":
                            console.log(JSON.stringify(user_info));
                            app.openWin('shopChat', {
                                otherId: user_info.openid, //id
                                nickname: user_info.nickname, //昵称
                                src: user_info.avater, //头像
                                new_nickname: user_info.new_nickname, //备注
                                openid2: user_info.openid
                            }, {
                                reload: true,
                            });
                            break;
                        case "shopSend":
                            //--发货
                            // app.openWin('shopSend', {shopId: that.shopId, order_id: order_id});
                            api.openFrame({
                                name: 'shopSend',
                                url: '../shop_send/shop_send.html',
                                rect: {
                                    x: 0,
                                    y: 0,
                                    w: 'auto',
                                    h: 'auto'
                                },
                                pageParam: {
                                    shopId: that.shopId,
                                    order_id: order_id
                                },
                                reload: true
                            });
                            break;
                        case "shopDel":
                            dialog.alert({
                                title: "订单回收提示",
                                msg: '请确认是否删除该订单',
                                buttons: ['取消', '确定']
                            }, function(ret) {
                                if (ret.buttonIndex == 2) { //确认
                                    app.ajax(service['shopDelOrder'], {
                                            data: {
                                                values: {
                                                    unionid: unionid,
                                                    order_id: order_id
                                                }
                                            }
                                        },
                                        function(res) {
                                            if (res.code) {
                                                alert("错误代码code：" + res.code + "\n错误信息：" + res.message)
                                            } else {
                                                console.log(res.data);
                                                api.sendEvent({
                                                    name: 'close_win_refresh'
                                                });
                                                api.closeWin();
                                                api.toast({
                                                    msg: '订单已删除',
                                                    duration: 2000,
                                                    location: 'middle'
                                                });
                                            }
                                        });
                                }
                            });
                            break;
                        case "shopFlow":
                            app.openWin('shopFlow', {
                                shopId: that.shopId,
                                order_id: order_id
                            });
                            break;
                        case "shopRefundRefuse":
                            app.openWin('shopRefundRefuse', {
                                shopId: that.shopId,
                                order_id: order_id
                            });
                            break;
                        case "shopRefundPass":
                            //--商家确认退款提示框
                            $('.shop_order_refund').css("display", "block");
                            break;
                        case "shopReturnRefuse":
                            app.openWin('shopReturnRefuse', {
                                shopId: that.shopId,
                                order_id: order_id
                            });
                            break;
                        case "shopReturnconfirm":
                            // --商家同意申请退款但此时并不退款到买家
                            dialog.alert({
                                title: "通过申请提示",
                                msg: '是否通过退货退款申请',
                                buttons: ['取消', '确定']
                            }, function(ret) {
                                // alert(JSON.stringify(ret));
                                if (ret.buttonIndex == 2) { //  确认
                                    //--调用确认退款事件
                                    var unionid = $api.getStorage('shop_unionid');
                                    var order_return_id = that.order_return_id;

                                    app.ajax(service['agreeReturnOrder'], {
                                            data: {
                                                values: {
                                                    unionid: unionid,
                                                    order_return_id: order_return_id
                                                }
                                            }
                                        },
                                        function(res) {
                                            if (res.code) {
                                                alert("错误代码code：" + res.code + "\n错误信息：" + res.message)
                                            } else {
                                                console.log("确认退货成功");
                                                console.log(JSON.stringify(res.data));

                                                that.orderDetailGet(function(res) {
                                                    that.orderDetailList = res.data;
                                                    that.refund_arr = that.orderDetailList.refund;
                                                    //--刷新订单列表的数据
                                                    api.sendEvent({
                                                        name: 'close_win_refresh'
                                                    });
                                                });
                                            }
                                        });
                                }
                            });
                            break;
                        case "shopReturnPass":
                            // --商家确认退款提示框
                            $('.shop_order_return').css("display", "block")
                            break;
                        default:
                            break;
                    }
                },
                //------------------------------------
                //--关闭确认退款确认框(点击模态框事件)
                close_shop_order_refund: function() {
                    $('.shop_order_refund').css("display", "none");
                },
                //--关闭确认退款确认框(点击取消按钮)
                closeRefund: function() {
                    $('.shop_order_refund').css("display", "none");
                },
                //--确认退款事件
                passRefund: function() {
                    var that = this;

                    var unionid = $api.getStorage('shop_unionid');
                    var order_return_id = this.order_return_id;

                    app.ajax(service['agreeReturnOrder'], {
                            data: {
                                values: {
                                    unionid: unionid,
                                    order_return_id: order_return_id
                                }
                            }
                        },
                        function(res) {
                            if (res.code) {
                                alert("错误代码code：" + res.code + "\n错误信息：" + res.message)
                            } else {
                                console.log("确认退款成功");
                                console.log(JSON.stringify(res.data));
                                //--关闭确认退款提示框
                                $('.shop_order_refund').css("display", "none");
                                api.sendEvent({
                                    name: 'close_win_refresh'
                                });
                                api.closeFrame();
                            }
                        });
                },
                //------------------------------------
                //--关闭确认退货确认框(点击模态框事件)
                close_shop_order_return: function() {
                    $('.shop_order_return').css("display", "none");
                },
                //--关闭确认退货确认框(点击取消按钮)
                closeReturn: function() {
                    $('.shop_order_return').css("display", "none");
                },
                //--确认退货事件
                passReturn: function() {
                    var that = this;

                    var unionid = $api.getStorage('shop_unionid');
                    var order_return_id = this.order_return_id;

                    app.ajax(service['agreeReturnOrder'], {
                            data: {
                                values: {
                                    unionid: unionid,
                                    order_return_id: order_return_id
                                }
                            }
                        },
                        function(res) {
                            if (res.code) {
                                alert("错误代码code：" + res.code + "\n错误信息：" + res.message)
                            } else {
                                console.log("确认退货成功");
                                console.log(JSON.stringify(res.data));
                                //--关闭确认退款提示框
                                $('.shop_order_return').css("display", "none");
                                api.sendEvent({
                                    name: 'close_win_refresh'
                                });
                                api.closeWin();
                            }
                        });
                },


            },
            created: function() {
                var that = this;

                this.order_id = api.pageParam.order_id;
                // this.goods_id = api.pageParam.goods_id;
                this.shopId = api.pageParam.shopId;

                this.orderDetailGet(function(res) {
                    that.orderDetailList = res.data;
                    var goods_arr = that.orderDetailList.goods;

                    that.refund_arr = that.orderDetailList.refund;
                    if (JSON.stringify(that.refund_arr) == "{}" || that.refund_arr == null) {
                        that.refund_arr = '';
                    }else{
                      that.refundPrice = that.refund_arr.price;
                      that.order_return_id = that.refund_arr.order_return_id;
                    }


                    console.log(JSON.stringify(that.orderDetailList));
                });

            }
        };
        // var shopVue = new Vue(vm);
        var even = function() {
            $(".header-btn").on('click', function() {
                api.closeFrame();
            })
        };
        var newVue = new Vue(vm);
        even();

    })
}
